const app =getApp();
Page({
  data: {
    user:{},
    txt:'',
    cardCur: 0,
    page:1,
    total:0,
    typeId:'',
    moduleCooperId:0,
    moduleCooperIndex:0,
    blindBoxList: [],
    boxList:[],
    collectShow:false,
    swiperList: [
      {
        id: 0,
        url: '../../images/nav.png'
      }, {
        id: 1,
        url: '../../icon/bar.png',
      }
    ]
  },
  onLoad: function (options) {

    this.getInfo();
    this.getTypeInfo();
  },
  // 上拉加载
	onReachBottom: function() {
    var that = this;
    this.data.page = this.data.page+1
      //显示加载动画
      wx.showLoading({
        title:"刷新中",
        mask:true
      })
      app.util.request({
        url: app.do('index/typeList'),
        method: 'Post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          typeId: that.data.moduleCooperId,
          page:that.data.page
        },
        success: function (res) {
          wx.hideLoading()//关闭加载动画
          if (res.data.code == "0000") {
            let dataList = res.data.data.typeList;
            if(dataList.length>0){
            //  es6解构
              that.data.boxList=[...that.data.boxList,...dataList]
              that.setData({
                boxList : that.data.boxList
              })
            }
          } else if (res.data.code == "2001") {
            wx.navigateTo({
              url: '../auth/auth',
            })
          }
        },
        fail: function (res) {
          wx.hideLoading({})
          var text = res.data.message;
          wx.showToast({
            title: text,
            icon: 'none',
            duration: 2000,
          })
        }
      })
	},
  getInfo(){
    let token = wx.getStorageSync('token')
    var that = this;
    app.util.request({
      url: app.do('user/Userinfo'),
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: token,
      },
      success: function (res) {
       
        if (res.data.code == "0000") {
          that.setData({
            user:res.data.data.user,
            txt:res.data.data.activity.about
          })
        } else if (res.data.code == "2001") {
          wx.navigateTo({
            url: '../auth/auth',
          })
        } else {
          var text = res.data.message;
          wx.showToast({
            title: text,
            icon: 'none',
            duration: 2000,
          })
        }
      },
      fail: function (res) {
        wx.hideLoading()
        var text = res.data.message;
        wx.showToast({
          title: text,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },
  //获取首页数据
  getTypeInfo() { 
    var that = this;
    var token = wx.getStorageSync('token');
    app.util.request({
      url: app.do('index/index'),
      method: 'Post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: token
      },
      success: function (res) {
        if (res.data.code == "0000") {
          that.setData({
            blindBoxList: res.data.data.boxType,
          })
          that.data.moduleCooperId = res.data.data.boxType[0].id
          console.log('', that.data.moduleCooperId )
          that.getTyptlist(that.data.moduleCooperId)
          // that.getTyptlist(that.data.moduleCooperId,that.data.page)
          
        } else if (res.data.code == "2001") {
          wx.navigateTo({
            url: '../auth/auth',
          })
        }
  
      },
      fail: function (res) {
        var text = res.data.message;
        wx.showToast({
          title: text,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },
  getTyptlist(typeId){
    var that = this;
    app.util.request({
      url: app.do('index/typeList'),
      method: 'Post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        typeId: typeId,
        page:1
      },
      success: function (res) {
        if (res.data.code == "0000") {
          that.setData({
            boxList: res.data.data.typeList,
            total: res.data.data.total
          })
        } else if (res.data.code == "2001") {
          wx.navigateTo({
            url: '../auth/auth',
          })
        }
 
      },
      fail: function (res) {
        var text = res.data.message;
        wx.showToast({
          title: text,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },
  // 切换盲盒分类
  toggle(e){
    this.data.page = 1;
  
    var that = this
    this.setData({
      moduleCooperIndex: e.currentTarget.dataset.index,
      moduleCooperId: e.currentTarget.dataset.id
    })
    this.getTyptlist(e.currentTarget.dataset.id,this.data.page)
},
  orderList(e){
    wx.navigateTo({
      url: '../orderList/orderList?index='+e.currentTarget.dataset.id,
    })
  },
  lookAddress(){
    wx.chooseAddress({
      success: (result3) => {
        // 获取数据
        this.setData({
          user:{
            name:result3.userName,
            cityName:result3.cityName
          }
        })
      }
    });
  },
  // 收集
  hideCollectCover(){
    this.setData({
      collectShow: false
    })
  },
  showCollectCover(e){
    this.setData({
      collectShow:true,
      typeId:e.currentTarget.dataset.id,
    })
    var that = this;
    let token = wx.getStorageSync('token')
    app.util.request({
      url: app.do('index/getSeries'),
      method: 'Post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        id: e.currentTarget.dataset.id,
        token: token,
      },
      success: function (res) {
        if (res.data.code == "0000") {
          that.setData({
            datailData: res.data.data.seriesDetail,
            boxsList:res.data.data.newData,
          })
          //解析富文本

          var content = that.data.content;
          var WxParse = require('../../../wxParse/wxParse.js');
          WxParse.wxParse('content', 'html', content, that,5)
        } 
      },
      fail: function (res) {
        var text = res.data.message;
        wx.showToast({
          title: text,
          icon: 'none',
          duration: 2000,
        })
      }
    })

  },
  //
  collectList(e){
    wx.navigateTo({
      url: '../choose/choose?id='+this.data.typeId
    })
  }

})